<?
require 'global.php';
if($customer_id==0) { header('Location:login.html'); exit; }

// 是国外的还是国内的
$overseas=OVERSEA;

$url='address_book';
$address_book=new DB(Taddress_book,$conn);
$gee=$address_book;

// refer page 来源页面
$refer='none';
if(isset($_POST["refer"])) $refer=clean($_POST["refer"]);
if(isset($_GET["refer"])) $refer=clean($_GET["refer"]);
$source='none';	if(isset($_GET["source"])) $source=clean($_GET["source"]);

$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除 
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD 
$id=0; if(isset($_GET["id"])) $id = clean($_GET["id"]); 

if($action=='insert'){ // sql动作: 添加
	$arr=array(
		'customer_id'=>$customer_id,
	);
	if(isset($_POST['country_id'])) $arr['country_id']=$_POST['country_id'];
	if(isset($_POST['province_id'])) $arr['province_id']=$_POST['province_id'];
	if(isset($_POST['city_id'])) $arr['city_id']=$_POST['city_id'];
	if(isset($_POST['county_id'])) $arr['county_id']=$_POST['county_id'];
	if(isset($_POST['street'])) $arr['street']=$_POST['street'];
	if(isset($_POST['zip'])) $arr['zip']=$_POST['zip'];
	if(isset($_POST['contact_name'])) $arr['contact_name']=$_POST['contact_name'];
	if(isset($_POST['mobile'])) $arr['mobile']=$_POST['mobile'];
	if($gee->insert($arr)) {
		// 是否来自订单页面 from order page?
		if($refer=='order'){ wheel('order.php?address_id='.$gee->get_last_insert_id()); exit; }
		yell('success',$url.'.php');
	}
	else {}
}
if($action=='update'){ // sql动作: 更新
	$arr=array();
	$temid=$_POST['temid'];
	if(isset($_POST['country_id'])) $arr['country_id']=$_POST['country_id'];
	if(isset($_POST['province_id'])) $arr['province_id']=$_POST['province_id'];
	if(isset($_POST['city_id'])) $arr['city_id']=$_POST['city_id'];
	if(isset($_POST['county_id'])) $arr['county_id']=$_POST['county_id'];
	if(isset($_POST['street'])) $arr['street']=$_POST['street'];
	if(isset($_POST['zip'])) $arr['zip']=$_POST['zip'];
	if(isset($_POST['contact_name'])) $arr['contact_name']=$_POST['contact_name'];
	if(isset($_POST['mobile'])) $arr['mobile']=$_POST['mobile'];
	try{
		$gee->update($arr,'id',$temid);
		// 是否来自订单页面 from order page?
		if($refer=='order'){ wheel('order.php?address_id='.$temid); exit; }
		yell('success',$url.'.php');
	}
	catch(Exception $e){
		yell('Failed, please retry.',$url.'.php');
	}
}

if($do=='view'){ // 功能: 查看列表
	$info=$gee->get_rows(1024,'customer_id',$customer_id);
}
if($do=='update'){ // 功能: 更新
	$info=$gee->get_row($id);
}
if($do=='default'){
	$where=array();
	$where[]=array('name'=>'customer_id','val'=>$customer_id);
	$where[]=array('name'=>'id','val'=>$id,'opn'=>'!=');
	$tem=$gee->get_rows_advanced(1024,$where);
	$in_array=array();
	foreach($tem as $row)$in_array[]=$row['id'];
	$arr=array('is_default'=>0);
	$gee->update($arr,'id',$in_array); // 批量更新
	$arr=array('is_default'=>1);
	$gee->update($arr,'id',$id); // 设置默认
	wheel($url.'.php'); exit;
}
// seo
$cate_name=WEB_ADDRESS_BOOK;
$title=$cate_name.' - '.$config['SITE_TITLE_'.LANG];
$kwd=$title;
$dpn=$kwd;

// view 加载视图
require VIEW.$url.'.php';

// end of file
